#include "lvgl/lvgl.h"
#include "lvgl/demos/lv_demos.h"
#include "lv_drivers/display/fbdev.h"
#include "lv_drivers/indev/evdev.h"
#include <unistd.h>
#include <pthread.h>
#include <time.h>
#include <sys/time.h>
#include "./rfid/rfid.h"
#include <pthread.h>

#define DISP_BUF_SIZE (128 * 1024)

extern int flag;

lv_obj_t * label4 = NULL;

void bus_card(void);

static void my_event_btn1_cb(lv_event_t * event)
{
    flag = 3;
    printf("flag = 3\n");
}
static void my_event_btn2_cb(lv_event_t * event)
{
    flag = 2;
    printf("flag = 2\n");
}
static void my_event_btn3_cb(lv_event_t * event)
{
    flag = 1;
    printf("flag = 1\n");
}


int main(void)
{
    /*LittlevGL init*/
    lv_init();

    /*Linux frame buffer device init*/
    fbdev_init();

    /*A small buffer for LittlevGL to draw the screen's content*/
    static lv_color_t buf[DISP_BUF_SIZE];

    /*Initialize a descriptor for the buffer*/
    static lv_disp_draw_buf_t disp_buf;
    lv_disp_draw_buf_init(&disp_buf, buf, NULL, DISP_BUF_SIZE);

    /*Initialize and register a display driver*/
    static lv_disp_drv_t disp_drv;
    lv_disp_drv_init(&disp_drv);
    disp_drv.draw_buf   = &disp_buf;
    disp_drv.flush_cb   = fbdev_flush;
    disp_drv.hor_res    = 800;
    disp_drv.ver_res    = 480;
    lv_disp_drv_register(&disp_drv);

    evdev_init();
    static lv_indev_drv_t indev_drv_1;
    lv_indev_drv_init(&indev_drv_1); /*Basic initialization*/
    indev_drv_1.type = LV_INDEV_TYPE_POINTER;

    /*This function will be called periodically (by the library) to get the mouse position and state*/
    indev_drv_1.read_cb = evdev_read;
    lv_indev_t *mouse_indev = lv_indev_drv_register(&indev_drv_1);


    /*Set a cursor for the mouse*/
    LV_IMG_DECLARE(mouse_cursor_icon)
    lv_obj_t * cursor_obj = lv_img_create(lv_scr_act()); /*Create an image object for the cursor */
    lv_img_set_src(cursor_obj, &mouse_cursor_icon);           /*Set the image source*/
    lv_indev_set_cursor(mouse_indev, cursor_obj);             /*Connect the image  object to the driver*/

    pthread_t  thread;
    pthread_create(&thread, NULL,
                          rfid_system, NULL);

    
    /*Create a Demo*/
    //lv_demo_widgets();
    bus_card();
    /*Handle LitlevGL tasks (tickless mode)*/
    while(1) {
        lv_timer_handler();
        usleep(5000);
    }

    return 0;
}

/*Set in lv_conf.h as `LV_TICK_CUSTOM_SYS_TIME_EXPR`*/
uint32_t custom_tick_get(void)
{
    static uint64_t start_ms = 0;
    if(start_ms == 0) {
        struct timeval tv_start;
        gettimeofday(&tv_start, NULL);
        start_ms = (tv_start.tv_sec * 1000000 + tv_start.tv_usec) / 1000;
    }

    struct timeval tv_now;
    gettimeofday(&tv_now, NULL);
    uint64_t now_ms;
    now_ms = (tv_now.tv_sec * 1000000 + tv_now.tv_usec) / 1000;

    uint32_t time_ms = now_ms - start_ms;
    return time_ms;
}

void bus_card(void)
{
    //****************【创建图片部件】****************/
    //声明图片
    LV_IMG_DECLARE(nailong);
    //创建图片部件
    lv_obj_t * img1 = lv_img_create(lv_scr_act());
    //给图片img1设置图片来源
    lv_img_set_src(img1, &nailong);

    //*****************************【创建按钮】**********************************/
    lv_obj_t * btn1 = lv_btn_create(lv_scr_act());
    //设置按钮的大小 宽=200，高=100
    lv_obj_set_size(btn1, 200, 100);
    //设置按钮的位置 x = 100, y = 100
    lv_obj_align(btn1, LV_ALIGN_TOP_MID, -200, 100);
    //*************【创建标签显示文本】****************/
    //标签是以按钮为父对象，则标签就在按钮的内部
    lv_obj_t * label1 =  lv_label_create(btn1);
    //设置标签文本
    lv_label_set_text(label1, "Recharge");//recharge : 充值
    //设置标签中央对其
    lv_obj_center(label1);
    
    lv_obj_add_event_cb(btn1, my_event_btn1_cb, LV_EVENT_CLICKED, NULL);

    //****************************【创建按钮】************************************/
    lv_obj_t * btn2 = lv_btn_create(lv_scr_act());
    //设置按钮的大小 宽=200，高=100
    lv_obj_set_size(btn2, 200, 100);
    //设置按钮的位置 x = 100, y = 100
    lv_obj_align(btn2, LV_ALIGN_TOP_MID, 200, 100);
    //*************【创建标签显示文本】****************/
    //标签是以按钮为父对象，则标签就在按钮的内部
    lv_obj_t * label2 =  lv_label_create(btn2);
    //设置标签文本
    lv_label_set_text(label2, "Register");//Register : 注册
    //设置标签中央对其
    lv_obj_center(label2);

    lv_obj_add_event_cb(btn2, my_event_btn2_cb, LV_EVENT_CLICKED, NULL);
    //****************************【创建按钮】************************************/
    lv_obj_t * btn3 = lv_btn_create(lv_scr_act());
    //设置按钮的大小 宽=200，高=100
    lv_obj_set_size(btn3, 200, 100);
    //设置按钮的位置 x = 100, y = 100
    lv_obj_align(btn3, LV_ALIGN_BOTTOM_MID, -200, -100);
    //*************【创建标签显示文本】****************/
    //标签是以按钮为父对象，则标签就在按钮的内部
    lv_obj_t * label3 =  lv_label_create(btn3);
    //设置标签文本
    lv_label_set_text(label3, "Cancel");//cancel : 注销
    //设置标签中央对其
    lv_obj_center(label3);
    lv_obj_add_event_cb(btn3, my_event_btn3_cb, LV_EVENT_CLICKED, NULL);


    lv_obj_t * obj = lv_obj_create(lv_scr_act());
    //设置按钮的大小 宽=200，高=100
    lv_obj_set_size(obj, 200, 100);
    //设置按钮的位置 x = 100, y = 100
    lv_obj_align(obj, LV_ALIGN_BOTTOM_RIGHT, -200, -100);
    //*************【创建标签显示文本】****************/
    //标签是以按钮为父对象，则标签就在按钮的内部
    label4 =  lv_label_create(obj);
    //设置标签文本
    lv_label_set_text(label4, "*******");//cancel : 注销
    //设置标签中央对其
    lv_obj_center(label4);
    
}